package com.aasa.siccosap.dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import oracle.jdbc.OracleTypes;

import org.apache.log4j.Logger;


import com.aasa.siccosap.bean.ClienteBean;
import com.aasa.siccosap.util.Constantes;

public class ConsultaClienteDAO extends ConsultaClienteBaseDAO{
	
	static Logger logger = Logger.getLogger("ConsultaClienteDAO.class");
	public ConsultaClienteDAO()	throws accesoDaoException {
		super();
	}
	
	public ConsultaClienteDAO(DataSource dataSource) {
		super(dataSource);
	}

	public List<ClienteBean> consultarClientes(ClienteBean objLCliente) throws Exception{
		
		ResultSet objL_rs = null;
        this.av_message = null;
        this.storedProcedure = "begin SICCOSPK_PROYECTO.SICCOSSP_BUSCAR_CLIENTES(?,?,?,?); end;";
		
		ArrayList<ClienteBean> aobjL_listaCliente = new ArrayList<ClienteBean>();
	
		try {
			
			 this.conn = dataSource.getConnection();
             this.stmt = this.conn.prepareCall(storedProcedure);
                           
             configuraParametrosConsultaCliente(objLCliente);
             
             this.stmt.execute();

             this.av_message = (String)stmt.getObject(1);                	
                 if(this.av_message.trim().toUpperCase().endsWith(Constantes.NO_ERRORS)){
                   
                	 objL_rs = (ResultSet)stmt.getObject(2);
                     while(objL_rs.next()){
                     	
                    	 ClienteBean objL_clienteBean = new ClienteBean();
                    	 objL_clienteBean.setStrPb_codigo(objL_rs.getString("CODCLI"));
                    	 objL_clienteBean.setStrPb_nombre(objL_rs.getString("NOMBRE"));
                    	 objL_clienteBean.setStrPb_ruc(objL_rs.getString("RUCCLI"));
                    	 
                    	 aobjL_listaCliente.add(objL_clienteBean);
                        	 
                     }
             }else{
                 return null;
             }
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		finally{
			
			close(this.conn, objL_rs, stmt);
		}
		
		return aobjL_listaCliente;
		
		
		
		
	}

	public void enviarCorreoCertificados(String strA_mailTo, String strA_mailSubject,
			String strA_mailCc, String strA_mailBody) throws Exception{
		// TODO Auto-generated method stub
		ResultSet objL_resultSet = null;
        this.av_message = null;
        this.storedProcedure = "begin SICCOSPK_PROYECTO.SICCOSSP_ENVIAR_SOLICITUD(?,?,?,?); end;";
		
		try {
			
		    this.conn = dataSource.getConnection();
            this.stmt = this.conn.prepareCall(storedProcedure);
         	stmt.setString(1,strA_mailTo);
         	stmt.setString(2,strA_mailSubject);
         	stmt.setString(3,strA_mailCc);
         	stmt.setString(4,strA_mailBody);
            this.stmt.execute();
            
            
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		finally{
			
			close(this.conn, objL_resultSet, stmt);
		}
		
	}



}
